package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author guoxiaolin
 * @description
 * @since 2019/3/27 15:10
 */
public class ProxyTask  extends AbstractFileTask implements FileTask {

    public ProxyTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderProxyFile = processingEnv.getFiler().
                createSourceFile(packageName+".proxy.ProxyService"+sufixName);

        try (PrintWriter out = new PrintWriter(builderProxyFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".proxy;");
                out.println();
            }

            out.println("import com.hx.battery.common.annotation.EntityService;");
            out.println("import com.hx.battery.common.hibernate.proxy.AbstractProxyService;");
            out.println("import com.hx.battery.common.hibernate.proxy.feign.IFeignProxyService;");
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import "+packageName+".proxy.feign.IFeignProxyService"+sufixName+";");
            out.println("import "+packageName+".service."+simpleClassName+";");
            out.println("import org.springframework.beans.factory.annotation.Autowired;");
            out.println("import org.springframework.stereotype.Service;");
            out.println();
            out.println("@EntityService("+sufixName+".class)");
            out.println("@Service");
            out.println("public class ProxyService"+sufixName+" " +
                    "extends AbstractProxyService<"+sufixName+"> implements "+simpleClassName+" { ");
            out.println();
            out.println("    private IFeignProxyService"+sufixName + " feignProxyService"+sufixName+";");
            out.println("    @Autowired");
            out.println("    public ProxyService"+sufixName+"(IFeignProxyService"+sufixName+" feignProxyService"+sufixName+"){");
            out.println("        this.feignProxyService"+sufixName+" = feignProxyService"+sufixName+";");
            out.println("    }");
            out.println("    @Override");
            out.println("    protected IFeignProxyService<"+sufixName+"> getService() {");
            out.println("        return feignProxyService"+sufixName+";");
            out.println("    }");
            out.println();
            if (setterMap.size() == 0){
                setterMap.forEach((methodName, argumentType) -> {
                    String args[] = argumentType.split(",");
                    String returnType = args[0];
                    int sumargs = args.length;
                    out.println("    @Override");
                    out.print("    public ApiResult " + methodName + " ");
                    out.print("(");
                    String argline = "";
                    for (int i = 1; i < sumargs; i++){
                        if (i<sumargs-1){
                            argline = argline + "a" + i + ", ";
                            out.print(args[i] + " a" + i + ",");
                        }else{
                            argline = argline + "a" + i;
                            out.print(args[i] + " a" + i);
                        }
                    }
                    out.println(") {");
                    out.println("        ApiResult add = proxy.add(" + argline + ");");
                    out.println("        return (int)add.getData();");
                    out.println("    }");
                });
            }

            out.println();
            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("ProxyTask of " + simpleClassName + " is over");
        }
    }
}
